package com.ruoyi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * 观察者模式
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    //外部可订阅的socket数据服务(socket/topic为服务端广播消息的客户端订阅地址前缀，socket/user为客户端个性化消息的订阅地址前缀)
    private static final String[] brokers = {
            "/wlwsys/socket",
    };

    // 消息代理(消息访问路径)
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker(brokers);//这是一个主题，是用来给客户端订阅使用的
        config.setApplicationDestinationPrefixes("/fox0g");//目的地点的前缀，这是客户端发往送信息往服务器的地址
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/wlwsys-socket")   //终端名称，也即服务器端名称,注册Stomp端点，websocket客户端订阅或发布消息前，需通过此端点(路径)先连接(握手)
                .setAllowedOrigins("*")            //允许跨域访问
                .withSockJS();                     //使用sockJS
    }

}

